<?php

namespace app\teacher\model;

use app\common\model\BaseModel;
use think\Model;

class CourseComment extends BaseModel
{
    public function tag()
    {
        return $this->belongsToMany(Tag::class, 'course_comment_tag', 'tag_id', 'course_comment_id');
    }

    public function reply()
    {
        return $this->hasMany(CourseReply::class, 'course_comment_id', 'course_comment_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'user_id', 'user_id')->field(['user_id', 'nickname', 'avatar', 'mobile']);
    }

    /**
     * 课程评论
     */
    public static function getComments($teacherCourseId, $size = 20)
    {
        $comments = self::with(['user', 'tag', 'reply'])->where(static::whereOnline())->where('teacher_course_id', $teacherCourseId)->order('createtime desc')->paginate($size);
        $res = self::dealData($comments->items());
        if ($res) {
            $res['total'] = $comments->total();
        }
        return $res;
    }

    /**
     * 处理数据
     */
    public static function dealData($comments)
    {
        if (!$comments) {
            return [];
        }
        array_walk($comments, function (&$val) {
            if ($val['user']) {
                $val['user']['mobile'] = substr_replace($val['user']['mobile'], '****', 3, 4);
            }
        });
        $count = count($comments);
        //效果满意度评分
        $resultScore = array_sum(array_column($comments, 'result_score')) / $count;
        $resultLike = $resultScore / 5 * 100;
        //服务满意度平分
        $serviceScore = array_sum(array_column($comments, 'service_score')) / $count;
        $serviceLike = $serviceScore / 5 * 100;
        //综合评分
        $comScore = ceil(array_sum(array_column($comments, 'com_score')) / $count);
        //标签
        $tag = Tag::getInTagId(CourseCommentTag::getByCourseId(array_column($comments, 'course_comment_id')));
        return [
            'comments' => $comments,
            'com_score' => $comScore,
            'result_like' => $resultLike % 2 ? numberFormat($resultLike).'%' : numberFormat($resultLike, 0).'%',
            'service_like' => $serviceLike % 2 ? numberFormat($serviceLike).'%' : numberFormat($serviceLike, 0).'%',
            'tag' => $tag
        ];
    }

    /**
     * 教师评分
     */
    public static function score($teacherCourseId)
    {
        $comments = self::where(static::whereOnline())->whereIn('teacher_course_id', $teacherCourseId)->select();
        $data = [
            'com_score' => 5,
            'result_like' => '100%',
            'service_like' => '100%'
        ];
        if ($comments) {
            $count = count($comments);
            //效果满意度评分
            $resultScore = array_sum(array_column($comments, 'result_score')) / $count;
            $resultLike = $resultScore / 5 * 100;
            $data['result_like'] = $resultLike % 2 ? numberFormat($resultLike).'%' : numberFormat($resultLike, 0).'%';
            //服务满意度平分
            $serviceScore = array_sum(array_column($comments, 'service_score')) / $count;
            $serviceLike = $serviceScore / 5 * 100;
            $data['service_like'] = $serviceLike % 2 ? numberFormat($serviceLike).'%' : numberFormat($serviceLike, 0).'%';
            //综合评分
            $comScore = ceil(array_sum(array_column($comments, 'com_score')) / $count);
            $data['com_score'] = $comScore;
        }
        return $data;
    }
}
